package com.example.hy2110szyx.controller;

import com.example.hy2110szyx.common.DatatablesJSON;
import com.example.hy2110szyx.common.MyJsonResult;
import com.example.hy2110szyx.entity.Customer;
import com.example.hy2110szyx.entity.FlightInfo;
import com.example.hy2110szyx.entity.UserInfo;
import com.example.hy2110szyx.service.CustomerService;
import com.example.hy2110szyx.vo.PageVo;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/customer")
@Slf4j
@CrossOrigin(origins = "*")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @GetMapping("/query")
    @ResponseBody
    public DatatablesJSON<Customer> query(
            int draw,
            @RequestParam("order[0][column]") int orderColumn,
            @RequestParam("order[0][dir]") String orderDir,
            int start, int length, String cno, String identity, String name
    ) {
        String[] columns = {null, "ccno", "cidentity", "cname", null, null, null, null};
        Customer customerConn = new Customer(cno, identity, name);
        log.debug("order[0][column]=" + orderColumn);
        log.debug("order[0][dir]=" + orderDir);
        log.debug("customerConn is {}", customerConn);
        PageVo<Customer> pageVo = new PageVo(start, length, columns[orderColumn], orderDir, customerConn);
        Page<Customer> page = customerService.queryAllCustomersForPage(pageVo);
        DatatablesJSON<Customer> datatablesJSON = new DatatablesJSON<>();
        datatablesJSON.setDraw(draw);
        datatablesJSON.setRecordsFiltered(page.getTotal());
        datatablesJSON.setData(page.getResult());
        return datatablesJSON;
    }

    @GetMapping("/query/{identity}")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult queryOneCustomer(@PathVariable("identity") String identity) {
        Customer customer = customerService.queryByIdentity(identity);
        return new MyJsonResult(0, null, null, customer);
    }

    /**刪除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delcustomer")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult delcustomer(@RequestBody Integer[] ids) {
        customerService.deleteSomeById(ids);
        return new MyJsonResult(0, "删除成功", null, null);
    }

    /**
     * 添加用户
     *
     * @param cust
     */
    @PostMapping("/addCustomer")
    @ResponseStatus(HttpStatus.CREATED)
    public MyJsonResult addOneCustomer(@RequestBody Customer cust) {
        Customer existsCustomer = null;
        String ccno = null;//客戶编号
        Customer customer = customerService.queryByIdentity(cust.getIdentity());
        if (null != customer) {
            return new MyJsonResult(1, "该客戶已存在", null, null);
        }
        do {
//            //防止用户编号重复
//            ccno = UUID.randomUUID().toString().replaceAll("-", "");
//            existsCustomer = customerService.queryByCno(ccno);
            //防止航班编号重复
           int random = (int) ((Math.random()*9+1)*1000000);
            log.debug("7位随机数:"+random);
            ccno = String.valueOf(random);
            existsCustomer = customerService.queryByCno(ccno);
        } while (null != existsCustomer);
        cust.setCno(ccno);
        customerService.insertCustomer(cust);
        return new MyJsonResult(0,"添加成功", null, cust);
    }
    @PutMapping("/modCustomer/{id}")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult modOneCustomer(@PathVariable Integer id, @RequestBody Customer customer) {
        customer.setId(id);
        customerService.updateOneCustomer(customer);
        return new MyJsonResult(0, null, null, customer);
    }
}

